package com.ocom.accesservice.controller;

import com.alibaba.fastjson.JSON;
import com.ocom.accesservice.service.GroupInfoService;
import com.ocom.accesservice.service.TimeInfoService;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.entity.access.EntityMjGroupInfo;
import com.ocom.common.entity.access.EntityMjTimeInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.GroupListVo;
import com.ocom.common.vo.access.GroupVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class GroupInfoController {

    @Autowired
    private GroupInfoService groupInfoService;

    @Autowired
    private TimeInfoService timeInfoService;

    //获取权限组列表
    @PostMapping(AccessServiceUrl.URL_GetGroupTimeList)
    public Result getGroupTimeList(Long companyID,String areaNo){

        try {
            if (companyID ==null || areaNo==null){
                return ResultUtil.error(ResultEnum.Err_203);
            }
            List<GroupListVo> list=groupInfoService.getGroupList(companyID, Long.valueOf(areaNo));


            List<EntityMjTimeInfo> timeInfoList=timeInfoService.getTimeInfoList(companyID, Long.valueOf(areaNo));

            List<Map<String,Object>> groupList=new ArrayList<>();
            for (GroupListVo groupListVo : list) {
                Map<String,Object> groupMap=new HashMap<>();
                groupMap.put("groupNo",groupListVo.getRoleI());
                groupMap.put("groupName",groupListVo.getGroupName());
                List<Long> zoneNoList=new ArrayList<>();
                Map<Long,Object> map=new HashMap<>();
                for (EntityMjTimeInfo entityMjTimeInfo : timeInfoList) {
                    if (entityMjTimeInfo.getGroupNo().compareTo(groupListVo.getRoleI())==0){
                        Long key=entityMjTimeInfo.getZoneNo();
                        //判断是否在时区
                        if (map.get(key)==null){
                            zoneNoList.add(key);
                            List<String> timeSlots=new ArrayList<>();
                            if (entityMjTimeInfo.getDirection()==1){
                                String strTimeSlot=entityMjTimeInfo.getBeginTime()+"-"+entityMjTimeInfo.getEndTime()+" 进";
                                timeSlots.add(strTimeSlot);
                            }else if (entityMjTimeInfo.getDirection()==2){
                                String strTimeSlot=entityMjTimeInfo.getBeginTime()+"-"+entityMjTimeInfo.getEndTime()+" 出";
                                timeSlots.add(strTimeSlot);
                            }else {
                                String strTimeSlot=entityMjTimeInfo.getBeginTime()+"-"+entityMjTimeInfo.getEndTime();
                                timeSlots.add(strTimeSlot);
                            }
                            map.put(key,timeSlots);
                        }else {
                            List<String> timeSlots= JSON.parseArray(JSON.toJSONString(map.get(key)),String.class);
                            if (entityMjTimeInfo.getDirection()==1){
                                String strTimeSlot=entityMjTimeInfo.getBeginTime()+"-"+entityMjTimeInfo.getEndTime()+" 进";
                                timeSlots.add(strTimeSlot);
                            }else if (entityMjTimeInfo.getDirection()==2){
                                String strTimeSlot=entityMjTimeInfo.getBeginTime()+"-"+entityMjTimeInfo.getEndTime()+" 出";
                                timeSlots.add(strTimeSlot);
                            }else {
                                String strTimeSlot=entityMjTimeInfo.getBeginTime()+"-"+entityMjTimeInfo.getEndTime();
                                timeSlots.add(strTimeSlot);
                            }
                            map.put(key,timeSlots);
                        }
                    }
                }
                List<Map<String,Object>> timeList=new ArrayList<>();
                for (Long aLong : zoneNoList) {
                    Map<String,Object> timeMap=new HashMap<>();
                    timeMap.put("zoneNo",aLong);
                    timeMap.put("timeSlots",map.get(aLong));
                    timeList.add(timeMap);
                }
                groupMap.put("timeList",timeList);
                groupList.add(groupMap);
            }
            Map<String,Object> reMap=new HashMap<>();

            reMap.put("groupList",groupList);


            return ResultUtil.success(reMap);
        }catch (Exception e){
            log.info("获取权限组列表异常:"+e);
            return ResultUtil.error(ResultEnum.Err_4002);
        }
    }


    //添加权限组
    @PostMapping(AccessServiceUrl.URL_addAccessGroup)
    public Result addAccessGroup(@RequestBody @Validated  GroupVo groupVo){
        EntityMjGroupInfo entityMjGroupInfo =groupInfoService.getAccessGroup(groupVo.getCompanyID(),groupVo);
        if (entityMjGroupInfo !=null){
            return ResultUtil.error(ResultEnum.Err_4042);
        }
        Integer i=groupInfoService.addGroup(groupVo.getCompanyID(),groupVo);
        if (i>0){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.Err_2016);
        }
    }

    //修改权限组名称（modifyGroupName）
    @PostMapping(AccessServiceUrl.URL_modifyGroupName)
    public Result modifyGroupName(@RequestBody @Validated  GroupVo groupVo){
        EntityMjGroupInfo entityMjGroupInfo =groupInfoService.selectName(groupVo.getCompanyID(),groupVo);
        if (entityMjGroupInfo !=null){
            return ResultUtil.error(ResultEnum.Err_4043);
        }
        Boolean i=groupInfoService.updateGroup(groupVo.getCompanyID(),groupVo);
        if (i){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.Err_3014);
        }
    }


    //获取权限组列表
    @PostMapping(AccessServiceUrl.Url_getGroupList)
    public Result<List<EntityMjGroupInfo>> getGroupList(Long companyId){
        return ResultUtil.success(groupInfoService.getGroupList(companyId));
    }
}
